/*
package com.micai.springboot.interceptor;

import com.micai.springboot.base.constant.Constants;
import com.micai.springboot.base.session.SessionProvider;
import com.micai.springboot.dto.UserDto;
import com.micai.springboot.util.UserAgentUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.Map;

*/
/**
 * 描述：后台-拦截器配置类
 * <p>
 * Author: 赵新国
 * Date: 2017/10/24 18:05
 *//*

public class WebInterceptor implements HandlerInterceptor {

    private static final Logger logger = LoggerFactory.getLogger(WebInterceptor.class);

    @Autowired
    private SessionProvider sessionProvider;

    // 常量
    private static final String INTERCEPTOR_URL = "/admin/";

    // 不拦截的URL
    private static final String [] NONE_AUTH_URL = {"/admin/index", "/admin/toLogin", "/admin/backLogin"};

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {

        */
/*if (UserAgentUtils.isMobileOrTablet(request)){
            logger.info("使用手机浏览器");
        } else {
            logger.info("使用web浏览器");
        }*//*


//        logger.info(">>>WebInterceptor>>>>>>>在请求处理之前进行调用（Controller方法调用之前）");
//        if (sessionProvider == null) {
//            sessionProvider = (SessionProvider) SpringUtils.getBean(SessionProvider.class);
//        }
        */
/**
         * 获取请求URL后面的参数名称和值
         *//*

//        Map<String, String[]> parameterMap = request.getParameterMap();
//        logger.info("parameterMap:{}", parameterMap);
//        Enumeration<String> parameterNames = request.getParameterNames();
//        while (parameterNames.hasMoreElements()) {
//            String nextElement = parameterNames.nextElement();
//            logger.info("parameterNames: {}", nextElement);
//            String[] parameterValues = request.getParameterValues(nextElement);
//            logger.info("parameterValues: {}", parameterValues);
//        }
        if (sessionProvider == null) {
            ServletContext servletContext = request.getServletContext();
            WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
            sessionProvider = webApplicationContext.getBean(SessionProvider.class);
        }
        UserDto userDto = (UserDto) sessionProvider.getAttribute(request, Constants.ADMIN_SESSION);
        boolean flag = false;
        if (null != userDto) {
            flag = true;
            request.setAttribute("admin", userDto);
        }
//        request.setAttribute("isLogin", flag);

        //是否拦截 http://localhost:8080/admin/login
        //	/admin/login
        String requestURI = request.getRequestURI();
        if (requestURI.startsWith(INTERCEPTOR_URL)) {
            //必须登录
            if(null == userDto){
                if (requestURI.contains(NONE_AUTH_URL[1])
                        || requestURI.contains(NONE_AUTH_URL[2])) {
                    return true;
                }
                response.sendRedirect("/admin/toLogin");
                return false;
            }
        }
        return true;// 只有返回true才会继续向下执行，返回false取消当前请求
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
//        logger.info(">>>WebInterceptor>>>>>>>请求处理之后进行调用，但是在视图被渲染之前（Controller方法调用之后）");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
//        logger.info(">>>WebInterceptor>>>>>>>在整个请求结束之后被调用，也就是在DispatcherServlet 渲染了对应的视图之后执行（主要是用于进行资源清理工作）");
    }

}
*/
